import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:old_shop/login&regist/view/button_view.dart';
import 'package:old_shop/login&regist/view/normal_text.dart';

import '../common/const/cosnt.dart';

class PwdLoginPage extends StatefulWidget {
  final Map params;

  const PwdLoginPage({Key? key, required this.params}) : super(key: key);
  @override
  _PwdLoginPageState createState() => _PwdLoginPageState();
}

class _PwdLoginPageState extends State<PwdLoginPage> {
  final FocusNode _phoneFocusNode = FocusNode();
  final FocusNode _pwdFocusNode = FocusNode();
  final TextEditingController _phoneController = TextEditingController();
  final TextEditingController _pwdController = TextEditingController();

  @override
  void initState() {
    super.initState();

    /// 防止网络请求不关闭
    EasyLoading.dismiss();
  }

  void _login(BuildContext context) {
    if (_phoneController.text.length != 11) {
      EasyLoading.showInfo("请输入11位手机号码");
      return;
    }
    EasyLoading.show(status: "Loading...");
    EasyLoading.dismiss();
    _jumpToRoot();
  }

  void _jumpToRoot() {
    Navigator.of(context).pushNamedAndRemoveUntil('/root', (Route<dynamic> route) => false);
  }

  void _unfocus() {
    _phoneFocusNode.unfocus();
    _pwdFocusNode.unfocus();
  }

  @override
  void dispose() {
    _phoneController.dispose();
    _pwdController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final size = MediaQuery.of(context).size;
    final marginH = size.height * 0.06;

    return Scaffold(
      backgroundColor: Colors.white,
      body: GestureDetector(
        behavior: HitTestBehavior.translucent,
        onTap: _unfocus,
        child: SafeArea(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              SizedBox(height: marginH),
              Padding(
                padding: const EdgeInsets.only(left: 15.0),
                child: Text(
                  "你好，\n欢迎来到我的商城！",
                  style: AppFont.textStyle(25, color: AppColor.font333, fontWeight: FontWeight.bold),
                ),
              ),
              Padding(
                padding: const EdgeInsets.symmetric(horizontal: 30),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    SizedBox(height: marginH + 22),
                    NormalTextfield(
                      hintText: "请输入手机号码",
                      backgroundColor: Colors.white,
                      inputFormatters: [FilteringTextInputFormatter.digitsOnly],
                      keyboardType: TextInputType.number,
                      focusNode: _phoneFocusNode,
                      maxLength: 11,
                      controller: _phoneController,
                    ),
                    SizedBox(height: 1, child: Divider()),
                    SizedBox(
                      height: 8,
                    ),
                    NormalTextfield(
                      hintText: "请输入密码",
                      backgroundColor: Colors.white,
                      obscureText: true,
                      maxLength: 20,
                      focusNode: _pwdFocusNode,
                      controller: _pwdController,
                    ),
                    SizedBox(height: 1, child: Divider()),
                  ],
                ),
              ),
              InkWell(
                  onTap: () => _login(context),
                  child: BottomButton(
                    margin: const EdgeInsets.only(top: 50, left: 15, right: 15),
                    title: "登录",
                  )),
              InkWell(
                onTap: () => Navigator.pushNamed(context, "/login", arguments: {"phone": _phoneController.text}),
                child: Align(
                  alignment: Alignment.centerRight,
                  child: Padding(
                    padding: const EdgeInsets.only(top: 18.0, right: 15, left: 15),
                    child: Text(
                      '使用手机验证码登录',
                      style: AppFont.textStyle(14, color: AppColor.mainColor),
                    ),
                  ),
                ),
              ),
              Spacer(),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  InkWell(
                      onTap: () => Navigator.pushNamed(context, "/regist"),
                      child: Text(
                        "立即注册",
                        style: AppFont.textStyle(14, color: AppColor.fontgrey),
                      )),
                  Container(
                    height: 16,
                    width: 1,
                    margin: const EdgeInsets.symmetric(horizontal: 20),
                    color: AppColor.fontgrey,
                  ),
                  InkWell(
                      onTap: () => Navigator.pushNamed(context, "/forgetpwd"),
                      child: Text(
                        "忘记密码",
                        style: AppFont.textStyle(14, color: AppColor.fontgrey),
                      )),
                ],
              ),
              SizedBox(
                height: 8,
              ),
            ],
          ),
        ),
      ),
    );
  }
}
